﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public static class ListExtension
{
    public static List<object> ToList<T>(this List<T> list, Func<T, object> predicate)
    {
        var l = from p in list
                select predicate(p);

        return l.ToList();
    }
    public static object Fist<T>(this List<T> list, Func<T, object> predicate)
    {
        return predicate(list[0]);
    }
    public static List<T> Pagging<T>(this List<T> source, int page, int pagesize)
    {
        List<T> list = new List<T>();
        int index = (page - 1) * pagesize;
        int count = source.Count;
        if (pagesize > count)
            list = source;
        else if (pagesize > count - index)
            list = source.GetRange(index, count - index);
        else
            list = source.GetRange(index, pagesize);
        return list;
    }
    public static List<T> Filter<T>(this List<T> source, Func<T, T, bool> predicate, Comparison<T> comparisonSort)
    {
        source.Sort(comparisonSort);
        List<T> list = new List<T>();
        int i = 0;
        while (i < source.Count - 1)
        {
            int j = i + 1;
            if (predicate(source[i], source[j]))
            {
                if (!list.Contains(source[i]))
                    list.Add(source[i]);
                if (!list.Contains(source[j]))
                    list.Add(source[j]);
            }
            i++;
        }
        return list;
    }

}